package com.jojo.android.zxlib.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;


import androidx.annotation.Nullable;

import com.jojo.android.zxlib.R;

import java.text.DecimalFormat;

/**
 * Created by Allen on 2017/5/14.
 * <p>
 * 自定义水平进度条
 */

public class HorizontalProgressBar extends View {

    private Paint bgPaint;
    private Paint progressPaint;
    private Paint bitmapPaint;
    private Paint tipsPaint;
    private Paint textPaint;

    /**
     * 进度
     */
    private float mTotalProgress;
    private float mProgress;

    /**
     * 当前进度
     */
    private float currentProgress;


    /**
     * 进度条画笔的宽度
     */
    private int progressPaintWidth;


    private Rect textRect = new Rect();
    private String textString = "准备中...";
    /**
     * 百分比文字字体大小
     */
    private int textPaintSize;
    /**
     * 提示文字字体大小
     */
    private int tipsPaintSize;

    /**
     * 进度条背景颜色
     */
    private int bgColor;
    /**
     * 进度条颜色
     */
    private int progressColor;
    /**
     * 圆角矩形的圆角半径
     */
    private int roundRectRadius;

    /**
     * 进度监听回调
     */
    private ProgressListener progressListener;
    private boolean isShowTips;
    private String tips;


    public HorizontalProgressBar(Context context) {
        super(context);
    }

    public HorizontalProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.HorizontalProgressBar);
        progressPaintWidth = typedArray.getDimensionPixelSize(R.styleable.HorizontalProgressBar_progressHeight, dp2px(1));
        roundRectRadius = typedArray.getDimensionPixelSize(R.styleable.HorizontalProgressBar_roundRectRadius, dp2px(5));
        tipsPaintSize = typedArray.getDimensionPixelSize(R.styleable.HorizontalProgressBar_progressTipsSize, sp2px(15));
        textPaintSize = typedArray.getDimensionPixelSize(R.styleable.HorizontalProgressBar_progressTextSize, sp2px(10));
        bgColor = typedArray.getColor(R.styleable.HorizontalProgressBar_progressBackgroundColor, 0xFFe1e5e8);
        progressColor = typedArray.getColor(R.styleable.HorizontalProgressBar_progressColor, 0xFFf66b12);
        initPaint();
    }


    /**
     * 初始化画笔
     */
    private void initPaint() {
        bgPaint = getPaint(progressPaintWidth, bgColor, Paint.Style.FILL);
        progressPaint = getPaint(progressPaintWidth, progressColor, Paint.Style.FILL);
        bitmapPaint = getPaint(1, Color.WHITE, Paint.Style.FILL);

        initTextPaint();
        initTipsPaint();
    }

    /**
     * 初始化文字画笔
     */
    private void initTextPaint() {
        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextSize(textPaintSize);
        textPaint.setColor(Color.WHITE);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setAntiAlias(true);
    }

    /**
     * 初始化tips画笔
     */
    private void initTipsPaint() {
        tipsPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        tipsPaint.setTextSize(tipsPaintSize);
        tipsPaint.setColor(Color.WHITE);
        tipsPaint.setTextAlign(Paint.Align.CENTER);
        tipsPaint.setAntiAlias(true);
    }

    /**
     * 统一处理paint
     *
     * @param strokeWidth
     * @param color
     * @param style
     * @return
     */
    private Paint getPaint(int strokeWidth, int color, Paint.Style style) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(strokeWidth);
        paint.setColor(color);
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStyle(style);
        return paint;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRoundRect(0, 0,
                getWidth(), getHeight(),
                roundRectRadius, roundRectRadius, bgPaint);
        canvas.drawRoundRect(0, 0,
                currentProgress, getHeight(),
                roundRectRadius, roundRectRadius, progressPaint);

        drawCenterText(canvas);

    }


    /**
     * 绘制进度条内部的文字
     */
    private void drawCenterText(Canvas canvas) {
        Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
        int baseline = (textRect.bottom + textRect.top - fontMetrics.bottom - fontMetrics.top) / 2;

        if (isShowTips) {
            canvas.drawText(tips, getWidth() / 2, (getHeight() / 2) + baseline, tipsPaint);
        } else {
            canvas.drawText(textString, getWidth() / 2, (getHeight() / 2) + baseline, textPaint);
        }
    }

    /**
     * 实时显示进度
     *
     * @param progress
     * @return
     */
    public HorizontalProgressBar setCurrentProgress(float progress) {
        currentProgress = (progress/mTotalProgress) * getWidth();
        mProgress = progress;
        postInvalidate();
        return this;
    }

    public void setTextString(String text){
        textString = text;
    }

    public void setTotalProgress(float totalProgress) {
        this.mTotalProgress = totalProgress;
    }

    /**
     * 如果true，那么进度条不显示分比进度，显示提示文字
     *
     * @param b
     */
    public void showTips(boolean b) {
        this.isShowTips = b;
    }

    public String getTips() {
        return tips;
    }

    public void setTips(String tips) {
        this.tips = tips;
    }

    /**
     * 回调接口
     */
    public interface ProgressListener {
        void currentProgressListener(float currentProgress);
    }

    /**
     * 回调监听事件
     *
     * @param listener
     * @return
     */
    public HorizontalProgressBar setProgressListener(ProgressListener listener) {
        progressListener = listener;
        return this;
    }

    /**
     * 格式化数字(保留两位小数)
     *
     * @param money
     * @return
     */
    public static String formatNumTwo(double money) {
        DecimalFormat format = new DecimalFormat("0.00");
        return format.format(money);
    }

    /**
     * 格式化数字(保留一位小数)
     *
     * @param money
     * @return
     */
    public static String formatNum(int money) {
        DecimalFormat format = new DecimalFormat("0");
        return format.format(money);
    }

    /**
     * dp 2 px
     *
     * @param dpVal
     */
    protected int dp2px(int dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, getResources().getDisplayMetrics());
    }

    /**
     * sp 2 px
     *
     * @param spVal
     * @return
     */
    protected int sp2px(int spVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                spVal, getResources().getDisplayMetrics());

    }


    public static int format2Int(double i) {
        return (int) i;
    }
}
